package com.ruoyi.outpatient.service.impl;

import java.util.List;

import com.ruoyi.common.utils.uuid.IdUtils;
import org.apache.xmlbeans.GDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.outpatient.domain.MedicalCostOutpatientInfo;
import com.ruoyi.outpatient.mapper.MedicalCostPersonArchivesMapper;
import com.ruoyi.outpatient.domain.MedicalCostPersonArchives;
import com.ruoyi.outpatient.service.IMedicalCostPersonArchivesService;
import java.util.Date;

/**
 * 个人档案Service业务层处理
 * 
 * @author yyj
 * @date 2022-01-03
 */
@Service
public class MedicalCostPersonArchivesServiceImpl implements IMedicalCostPersonArchivesService 
{
    @Autowired
    private MedicalCostPersonArchivesMapper medicalCostPersonArchivesMapper;

    /**
     * 查询个人档案
     * 
     * @param id 个人档案主键
     * @return 个人档案
     */
    @Override
    public MedicalCostPersonArchives selectMedicalCostPersonArchivesById(String id)
    {
        return medicalCostPersonArchivesMapper.selectMedicalCostPersonArchivesById(id);
    }

    /**
     * 查询个人档案列表
     * 
     * @param medicalCostPersonArchives 个人档案
     * @return 个人档案
     */
    @Override
    public List<MedicalCostPersonArchives> selectMedicalCostPersonArchivesList(MedicalCostPersonArchives medicalCostPersonArchives)
    {
        return medicalCostPersonArchivesMapper.selectMedicalCostPersonArchivesList(medicalCostPersonArchives);
    }

    /**
     * 新增个人档案
     * 
     * @param medicalCostPersonArchives 个人档案
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMedicalCostPersonArchives(MedicalCostPersonArchives medicalCostPersonArchives)
    {
        medicalCostPersonArchives.setId(IdUtils.fastSimpleUUID().substring(0,6));
        int rows = medicalCostPersonArchivesMapper.insertMedicalCostPersonArchives(medicalCostPersonArchives);
        insertMedicalCostOutpatientInfo(medicalCostPersonArchives);
        return rows;
    }

    /**
     * 修改个人档案
     * 
     * @param medicalCostPersonArchives 个人档案
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMedicalCostPersonArchives(MedicalCostPersonArchives medicalCostPersonArchives)
    {
        medicalCostPersonArchivesMapper.deleteMedicalCostOutpatientInfoByPersonid(medicalCostPersonArchives.getId());
        insertMedicalCostOutpatientInfo(medicalCostPersonArchives);
        return medicalCostPersonArchivesMapper.updateMedicalCostPersonArchives(medicalCostPersonArchives);
    }

    /**
     * 批量删除个人档案
     * 
     * @param ids 需要删除的个人档案主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMedicalCostPersonArchivesByIds(String[] ids)
    {
        medicalCostPersonArchivesMapper.deleteMedicalCostOutpatientInfoByPersonids(ids);
        return medicalCostPersonArchivesMapper.deleteMedicalCostPersonArchivesByIds(ids);
    }

    /**
     * 删除个人档案信息
     * 
     * @param id 个人档案主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMedicalCostPersonArchivesById(String id)
    {
        medicalCostPersonArchivesMapper.deleteMedicalCostOutpatientInfoByPersonid(id);
        return medicalCostPersonArchivesMapper.deleteMedicalCostPersonArchivesById(id);
    }

    /**
     * 新增门诊卡信息信息
     * 
     * @param medicalCostPersonArchives 个人档案对象
     */
    public void insertMedicalCostOutpatientInfo(MedicalCostPersonArchives medicalCostPersonArchives)
    {
        List<MedicalCostOutpatientInfo> medicalCostOutpatientInfoList = medicalCostPersonArchives.getMedicalCostOutpatientInfoList();
        String id = medicalCostPersonArchives.getId();
        if (StringUtils.isNotNull(medicalCostOutpatientInfoList))
        {
            List<MedicalCostOutpatientInfo> list = new ArrayList<MedicalCostOutpatientInfo>();
            for (MedicalCostOutpatientInfo medicalCostOutpatientInfo : medicalCostOutpatientInfoList)
            {
                long time = new Date().getTime();
                medicalCostOutpatientInfo.setId(time);
                medicalCostOutpatientInfo.setPersonid(id);
                list.add(medicalCostOutpatientInfo);
            }
            if (list.size() > 0)
            {
                medicalCostPersonArchivesMapper.batchMedicalCostOutpatientInfo(list);
            }
        }
    }
}
